package com.fau.om.action.index;

import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.om.action.PublicAction;
import com.fau.om.entity.MedicalAdmin;
import com.fau.om.entity.MedicalCallCustomer;
import com.fau.om.entity.MedicalOrders;
import com.fau.om.entity.MedicalOrdersStorage;
import com.fau.om.util.FileNameEncoderUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 订单审核Action
 * @author suny
 * @datetime 2014年9月15日,上午10:29:21
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class AuditAction extends PublicAction implements ModelDriven<MedicalOrders>{
	
	private static final long serialVersionUID = -6676099270584263067L;
	private MedicalOrders orders = new MedicalOrders();
	
	/**
	 * @desc 订单审核页面初始化
	 * @return String
	 * @author kangyj
	 * @datetime 2014年11月25日,下午11:23:50
	 */
	@Action(value = "audit_init", results = {@Result(name = SUCCESS, location = "audit_init.jsp")})
	public String init() {
		return SUCCESS;
	}
	
	/**
	 * @desc 分页查询订单审核
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月25日,下午11:27:17
	 */
	@Action(value = "audit_view")
	public String view() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		orders.setHospitalId(admin.getEnterpriseId());
		pageBean = this.baseAllService.getAuditManagerService().findPendingOrders(orders, map);
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 跳转到订单详情tabs页
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月26日,上午1:15:53
	 */
	@Action(value = "audit_orders_detail_init", results = {@Result(name = SUCCESS, location = "audit_orders_detail_init.jsp")})
	public String ordersDetailInit() {
		return SUCCESS;
	}
	
	/**
	 * @desc 订单详情页
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月26日,上午1:17:57
	 */
	@Action(value = "audit_orders_detail", results = {@Result(name = SUCCESS, location = "audit_orders_detail.jsp")})
	public String ordersDetail() {
		return SUCCESS;
	}
	
	/**
	 * @desc 根据id查询订单信息
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月26日,上午1:21:48
	 */
	@Action(value = "audit_orders_detail_view")
	public String ordersDetailView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		MedicalOrdersStorage ordersStorage = this.baseAllService.getAuditManagerService().findOrdersById(orders.getId());
		String backCount = this.baseAllService.getAuditManagerService().findBackCount(admin.getEnterpriseId());
		ordersStorage.setBackCount(backCount);
		map.put("ordersStorage", ordersStorage);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 根据订单id查询订单产品
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月26日,上午2:05:55
	 */
	@Action(value = "audit_orders_detail_product")
	public String findProductByOid() {
		HttpServletResponse response = ServletActionContext.getResponse();
		list = this.baseAllService.getAuditManagerService().findProductByOid(orders.getId());
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 客户信息页面
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月26日,上午1:20:59
	 */
	@Action(value = "audit_customer_detail", results = {@Result(name = SUCCESS, location = "audit_customer_detail.jsp")})
	public String customerDetail() {
		return SUCCESS;
	}
	
	/**
	 * @desc 根据id 查询客户信息
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月26日,上午1:23:12
	 */
	@Action(value = "audit_customer_detail_view")
	public String customerDetailView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalCallCustomer customer = this.baseAllService.getAuditManagerService().findCustomerById(orders.getCustomerId());
		map.put("customer", customer);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 订单审核通过
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月26日,上午10:30:47
	 */
	@Action(value = "audit_pass")
	public String auditPass() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if(StringUtils.isNotBlank(orders.getId())) {
			this.baseAllService.getAuditManagerService().auditOrdersPass(admin, orders);
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 订单审核不通过
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月26日,上午10:31:36
	 */
	@Action(value = "audit_no_pass")
	public String auditNoPass() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if(StringUtils.isNotBlank(orders.getId())) {
			this.baseAllService.getAuditManagerService().auditOrdersNoPass(admin, orders);
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 订单审核无效
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月26日,上午10:33:16
	 */
	@Action(value = "audit_invalid")
	public String auditInvalid() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if(StringUtils.isNotBlank(orders.getId())) {
			this.baseAllService.getAuditManagerService().auditOrdersInvalid(orders, admin);
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 订单审核-导出当前页待审核订单
	 * @return null
	 * @author dangjh
	 * @datetime 2014年10月20日,上午11:39:19
	 */
	@Action(value = "audit_init_export_current")
	public String currentExport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		String ids = request.getParameter("ids");
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"待审核订单表.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getAuditManagerService().exportOrdersCurrent(ids);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 待审核订单-导出所有待审核订单
	 * @return null
	 * @author dangjh
	 * @datetime 2014年10月21日,下午11:51:37
	 */
	@Action(value = "audit_init_export_all")
	public String allExport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"待审核订单表.xls"));
			response.setContentType("application/vnd.ms-excel");
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			orders.setHospitalId(admin.getEnterpriseId());
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getAuditManagerService().exportOrdersAll(orders);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 中心待审核订单-导出所有待审核订单
	 * @return null
	 * @author dangjh
	 * @datetime 2014年10月21日,下午11:51:37
	 */
	@Action(value = "audit_center_export_all")
	public String allCenterExport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"中心待审核订单表.xls"));
			response.setContentType("application/vnd.ms-excel");
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			orders.setHospitalId(admin.getEnterpriseId());
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getAuditManagerService().exportCenterOrdersAll(orders, admin);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 组待审核订单-导出所有待审核订单
	 * @return null
	 * @author dangjh
	 * @datetime 2014年10月21日,下午11:51:37
	 */
	@Action(value = "audit_group_export_all")
	public String allGroupExport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"组待审核订单表.xls"));
			response.setContentType("application/vnd.ms-excel");
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			orders.setHospitalId(admin.getEnterpriseId());
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getAuditManagerService().exportGroupOrdersAll(orders, admin);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 我的待审核订单-导出所有待审核订单
	 * @return null
	 * @author dangjh
	 * @datetime 2014年10月21日,下午11:51:37
	 */
	@Action(value = "audit_my_export_all")
	public String allMyExport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"我的待审核订单表.xls"));
			response.setContentType("application/vnd.ms-excel");
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			orders.setHospitalId(admin.getEnterpriseId());
			orders.setCheckUserId(admin.getId());
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getAuditManagerService().exportMyOrdersAll(orders, admin);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 验证订单是否存在及是否分配到话务员
	 * @return
	 * @author kangyj
	 * @datetime 2015年2月15日,下午2:36:56
	 */
	@Action(value = "audit_valid_orders")
	public String validOrders() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		String ordersStatus = this.baseAllService.getAuditManagerService().validOrders(orders.getId());
		map.put("ordersStatus", ordersStatus);
		formatJSON(response, map);
		return null;
	}
	
	@Override
	public MedicalOrders getModel() {
		return orders;
	}
}
